'use strict';

const BaseController = require('./base');

class HouseInfoController extends BaseController {
  get _service() {
    return 'houseInfo';
  }

  // 更新基本数据
  async update() {
    const { ctx } = this;
    const { content, pictures, houseArea, direction, floor, type, remarks, personNum, deviceIds, advantageIds } = ctx.params();
    const { id } = ctx.params;
    const result = await this._getService().update({ content, pictures, houseArea, direction, floor, type, remarks, personNum, deviceIds, advantageIds, status: 2, id });
    if (result) {
      this.success();
    } else {
      this.error('更新民宿信息失败');
    }
  }

  // 更新设备
  async editDevice() {
    const { ctx } = this;
    const { deviceIds, id } = ctx.params();
    const result = await this._getService().update({ deviceIds, id });
    if (result) {
      this.success();
    } else {
      this.error('更新民宿信息失败');
    }
  }

  // 更新特点
  async editAdvantage() {
    const { ctx } = this;
    const { advantageIds, id } = ctx.params();
    const result = await this._getService().update({ advantageIds, id });
    if (result) {
      this.success();
    } else {
      this.error('更新民宿信息失败');
    }
  }

  async agree() {
    const { ctx } = this;
    const { id } = ctx.params();
    const result = await this._getService().update({ status: 3, id });
    if (result) {
      this.success();
    } else {
      this.error('更新民宿信息失败');
    }
  }

  async disagree() {
    const { ctx } = this;
    const { id } = ctx.params();
    const result = await this._getService().update({ status: 4, id });
    if (result) {
      this.success();
    } else {
      this.error('更新民宿信息失败');
    }
  }

  async show() {
    const { ctx } = this;
    const { id } = ctx.params;
    const houseInfo = await this._getService().findOne({ id: +id });
    let device = {},
      advantage = {};
    if (houseInfo.deviceIds) {
      const tempDevice = await this.service.device.findAll({ ids: houseInfo.deviceIds });
      device = ctx.helper.convert(tempDevice);
    }
    if (houseInfo.advantageIds) {
      advantage = await this.service.advantage.findAll({ ids: houseInfo.advantageIds });
    }
    if (houseInfo) {
      this.success({ ...houseInfo, device, advantage });
    } else {
      this.error('获取房屋信息失败');
    }
  }

  // 获取所有民宿信息
  async index() {
    const { ctx } = this;
    const { minHouseArea, maxHouseArea, direction, floor, type, minxPersonNum, maxPersonNum, status, prop, sort, pageIndex, pageSize } = ctx.params();
    const { createdAt } = ctx.queries;
    const houseArea = ctx.helper.transformRange([ +minHouseArea || 0, +maxHouseArea || 10000 ]);
    const personNum = ctx.helper.transformRange([ +minxPersonNum || 0, +maxPersonNum || 10000 ]);
    const tempResult = await this._getService().findList({ houseArea, direction, floor, type, personNum, status, prop, sort, pageIndex, pageSize, createdAt });
    const result = ctx.helper.flat(tempResult.data);
    if (result) {
      this.success(result, tempResult.count);
    } else {
      this.error('获取民宿失败');
    }
  }

  // 获取登录人的民宿信息
  async indexMy() {
    const { ctx } = this;
    const { status, prop, sort, pageIndex, pageSize } = ctx.params();
    const { createdAt } = ctx.queries;
    const tempResult = await this._getService().findList({ status, oid: ctx.userId, prop, sort, pageIndex, pageSize, createdAt });
    const result = ctx.helper.flat(tempResult.data);
    if (result) {
      this.success(result, tempResult.count);
    } else {
      this.error('获取民宿失败');
    }
  }

  // 获取上架的房屋信息
  async indexShops() {
    const { ctx } = this;
    const { minHouseArea, maxHouseArea, direction, floor, type, minxPersonNum, maxPersonNum, prop, sort, pageIndex, pageSize } = ctx.params();
    const { createdAt } = ctx.queries;
    const houseArea = ctx.helper.transformRange([ +minHouseArea || 0, +maxHouseArea || 10000 ]);
    const personNum = ctx.helper.transformRange([ +minxPersonNum || 0, +maxPersonNum || 10000 ]);
    const tempResult = await this._getService().findList({ houseArea, direction, floor, type, personNum, status: 3, prop, sort, pageIndex, pageSize, createdAt });
    const result = ctx.helper.flat(tempResult.data);
    if (result) {
      this.success(result, tempResult.count);
    } else {
      this.error('获取民宿失败');
    }
  }

  async indexExamine() {
    const { ctx } = this;
    const { minHouseArea, maxHouseArea, direction, floor, type, minxPersonNum, maxPersonNum, prop, sort, pageIndex, pageSize } = ctx.params();
    const { createdAt } = ctx.queries;
    const houseArea = ctx.helper.transformRange([ +minHouseArea || 0, +maxHouseArea || 10000 ]);
    const personNum = ctx.helper.transformRange([ +minxPersonNum || 0, +maxPersonNum || 10000 ]);
    const tempResult = await this._getService().findList({ houseArea, direction, floor, type, personNum, status: 2, prop, sort, pageIndex, pageSize, createdAt });
    const result = ctx.helper.flat(tempResult.data);
    if (result) {
      this.success(result, tempResult.count);
    } else {
      this.error('获取民宿失败');
    }
  }
}

module.exports = HouseInfoController;
